using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BadassPhotoAlbum
{
    /******************************************************
     * Created By: Joseph Alfred 11/03/2011
     * Modified By: Steven Gonzalez 12/05/2011
     * This is the main form that handles the slide show.
     * The user will be able to skip ahead or back by one 
     * picture. When the user chooses to do so the slide
     * show will stop.
     * ***************************************************/
    public partial class SlideShowForm : Form
    {
        List<Photo> photoList;
        int photoIndex = 0;

        
        /// <summary>
        /// Constructors to pass along the selected album.
        /// Written by: Adam Reaves & Steven Gonzalez
        /// </summary>
        /// <param name="album"></param>
        public SlideShowForm(Album album)
        {
            if (album == null || album.getPhotoList().Count == 0)
            {
                this.Close();
                return;
            }
            InitializeComponent();
            photoList = album.getPhotoList();
            this.Text = album.AlbumName;

            this.KeyPreview = true; //enable capture of arrow keys
            this.MouseWheel += new MouseEventHandler(SlideShowForm_MouseWheel);

        }

        /// <summary>
        /// Constructors to pass along the selected album.
        /// Follows hierarchical construction. Accepts Start of show boolean.
        /// Written by: Adam Reaves & Steven Gonzalez
        /// </summary>
        /// <param name="album"></param>
        /// <param name="start"></param>
        public SlideShowForm(Album album, Boolean start)
            : this(album)
        {
            TimerStart();
        }

        /// <summary>
        /// Constructors to pass along the selected album.
        /// Follows hierarchical construction. Accepts Start of show boolean.
        /// An Index is passed to start at a specific photo.
        /// Written by: Adam Reaves & Steven Gonzalez
        /// </summary>
        /// <param name="album"></param>
        /// <param name="start"></param>
        /// <param name="startIndex"></param>
        public SlideShowForm(Album album, Boolean start, int startIndex)
            : this(album, start)
        {
            int photoCount = album.getPhotoList().Count;
            if (startIndex > photoCount)
                startIndex = photoCount - 1;
            else if (startIndex < 0)
                startIndex = 0;

            photoIndex = startIndex;
        }

        /// <summary>
        /// This function captures keyboard press events.
        /// The SlideShow will fire different events based on the key.
        /// Written by: Steven Gonzalez
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        protected override bool ProcessKeyPreview(ref Message m)
        {
            switch (Int32.Parse(m.WParam.ToString()))
            {
                case 37: //left arrow
                    TimerStop();
                    PictureLeft();
                    break;
                case 38: //up arrow
                    TimerStop();
                    PictureRight();
                    break;
                case 39: //right arrow
                    TimerStop();
                    PictureRight();
                    break;
                case 40: //down arror
                    TimerStop();
                    PictureLeft();
                    break;
            }
            return base.ProcessKeyPreview(ref m);
        }

        /// <summary>
        /// This function captures mouse wheel events.
        /// The SlideShow will fire different events based on the mouse.
        /// Written by: Adam Reaves
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void SlideShowForm_MouseWheel(object sender, MouseEventArgs e)
        {
            TimerStop();

            if (e.Delta > 0) //move left
                PictureLeft();

            else
                PictureRight();

        }
        /**********************************************
         * This function is used to handle the key press event
         * for going to the next or previous photo in the 
         * slide show.
         * *******************************************/
        /* private void slideShowForm_KeyPress(object sender, KeyPressEventArgs e)
          {
              switch (e.KeyCode)
              {
                  case Keys.Left:
                      pictureLeft();
                      break;

                  case Keys.Right:
                      pictureRight();
                      break;
              }
                       
          }
         */

        /**************************************
         * This function is used to control the stop of the timer.
         * The timer is disabeled and stops, the "text" on the button 
         * will then be changed to "Play"
         * Modified by: Steven Gonzalez
         * **********************************/
        private void TimerStop()
        {
            picTimer.Enabled = false;
            picTimer.Stop();
            try
            {
                playButton.Image = Image.FromFile(Application.StartupPath + "/Icons/Actions-media-playback-start-icon.png");
            }
            catch
            {
            }
            //pause.Text = "Play";
        }

        /************************************************
         * This is the function that is used to control the 
         * start timer for the slide show. The timer will be 
         * reenabeled and then will begin back at 2500miliseconds
         * The "text" on the buton will then be changed to "pause"
         * Modified by: Steven Gonzalez
         * **********************************************/
        private void TimerStart()
        {
            picTimer.Enabled = true;
            picTimer.Interval = 2500;
            picTimer.Start();
            try
            {
                playButton.Image = Image.FromFile(Application.StartupPath + "/Icons/Actions-media-playback-stop-icon.png");
            }
            catch
            {
            }
            //pause.Text = "Pause";
        }

        /*********************************************
        * This is the function that controls the slide show
        * moving "right" or the the next photo in the selected
        * album.
        * ******************************************/
        private void PictureRight()
        {
            if (photoList.Count == 0)
            {
                this.Close();
                return;
            }

            if (photoIndex + 1 > photoList.Count - 1)
            {
                photoIndex = 0;
            }
            else
            {
                photoIndex++;
            }
            //set the image in the textbox to the next photo in the selected album
            slideShowImage.Image = Image.FromStream(photoList[photoIndex].GetFileDescriptor());
        }

        /****************************************************
         * This is the function that controls the net or "previous"
         * portion of the slide show.
         * *************************************************/
        private void PictureLeft()
        {
            if (photoList.Count == 0)
            {
                this.Close();
                return;
            }

            if (photoIndex - 1 < 0)//if the image is at the first image then go to the end of the photo list
            {
                photoIndex = photoList.Count - 1;
            }
            else
            {
                photoIndex--;
            }
            slideShowImage.Image = Image.FromStream(photoList[photoIndex].GetFileDescriptor());
        }

        /***************************************************
         * when the form is loaded the slide show will commence
         * **************************************************/
        private void SlideShowFormLoad(object sender, EventArgs e)
        {
            //load the first image and set the timer
            slideShowImage.Image = Image.FromStream(photoList[photoIndex].GetFileDescriptor());
            picTimer.Start();
        }

        /****************************************************
         * This is the timer event that causes the timer to tick
         * When the timer begins the images will be begin to display from
         * the begining of the list.
         * **************************************************/
        private void PhotoTimerTick(object sender, EventArgs e)
        {
            PictureRight();
        }

        /*******************************
         * When the user clicks on the "prev" button
         * the timer will stop and the previous image
         * will be displayed
         * ******************************/
        private void LeftClick(object sender, EventArgs e)
        {
            TimerStop();
            PictureLeft();
        }

        /*******************************
         * When the user clicks on the "next" button
         * the timer will stop and the next image
         * will be displayed
         * ******************************/
        private void RightClick(object sender, EventArgs e)
        {
            TimerStop();
            PictureRight();
        }

        /*******************************
         * When the user clicks on the "pause/start" button
         * the timer will stop or start depending
         * on the status of the timer.
         * ******************************/
        private void PauseClick(object sender, EventArgs e)
        {
            if (picTimer.Enabled)
            {
                TimerStop();
            }
            else
            {
                TimerStart();
            }
        }
    }
}
